package com.shengzai.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class RowFilterData {
    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        Connection conn = ConnectionFactory.createConnection(conf);

        TableName tableName = TableName.valueOf("api_test:api_student");
        Table table = conn.getTable(tableName);

        RowFilter rowFilter = new RowFilter(
                CompareFilter.CompareOp.EQUAL,
                //根据RowKey正则判断
                new RegexStringComparator("^15001007.*"));

        Scan scan = new Scan();

        scan.setFilter(rowFilter);

        ResultScanner scanner = table.getScanner(scan);

        Result result;

        while ((result=scanner.next())!=null){
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String attribute = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowKey+"\t"+family+"\t"+attribute+"\t"+value);
            }
        }
    }
}
